# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 1: Read data (donor sample)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(readxl)

# Load relevant functions -------------------------------------------------

source("functions/read_Survey.R")

# Read data ---------------------------------------------------------------

survey <- read_Survey("data/BLW_Wave_8_Conjoint__Donors.csv") %>% 
  filter(Q3.1 != "", # finished at least one table
         Q1.1 == 1, # consent
         Q2.1 > 1, # above 18
         Q4.3 < 52) %>% # outside US
  filter(!(Q10.3 == 4 | Q10.3 == 5)) %>% # remove insincere respondents 
  filter(F.1.1 != "") # odd observations removed

# Respondent-level variables ----------------------------------------------

respondents <- survey %>% 
  rename(id = V1,
         start = V3,
         end = V4,
         age = Q2.1,
         state = Q4.3,
         gender = Q4.4,
         ideology = Q4.5,
         initial_partisanship = Q4.6,
         interest = Q8.6,
         trump = Q8.7,
         education = Q8.2,
         hispanic = Q8.4,
         race = Q8.5,
         lookup_info = Q8.8,
         honesty = Q10.2,
         insincere = Q10.3,
         comments = Q10.5) %>% 
  select(-contains("F."),
         -contains("K."),
         -contains("_1"),
         -contains("_2"),
         -contains("_3"),
         -contains("_4"),
         -V2,
         -V5,
         -Q1.1, -Q4.1) %>% 
  mutate(partisanship = NA,
         partisanship = ifelse(Q5.2 == 1 & !is.na(Q5.2), 1, partisanship), # strong republican
         partisanship = ifelse(Q5.2 == 2 & !is.na(Q5.2), 2, partisanship), # not very strong republican
         partisanship = ifelse(Q7.2 == 1 & !is.na(Q7.2), 3, partisanship), # republican leaner
         partisanship = ifelse(Q7.2 == 3 & !is.na(Q7.2), 4, partisanship), # true independent
         partisanship = ifelse(Q7.2 == 2 & !is.na(Q7.2), 5, partisanship), # democratic leaner
         partisanship = ifelse(Q6.2 == 2 & !is.na(Q6.2), 6, partisanship), # not very strong democrat
         partisanship = ifelse(Q6.2 == 1 & !is.na(Q6.2), 7, partisanship)  # strong democrat
  ) %>% 
  mutate(partisanship_dum = NA,
         partisanship_dum = ifelse(partisanship < 4 & !is.na(partisanship), 1, partisanship_dum), # republican = 1
         partisanship_dum = ifelse(partisanship > 4 & !is.na(partisanship), 2, partisanship_dum), # democrat = 2
         partisanship_dum = ifelse(partisanship == 4 & !is.na(partisanship), 3, partisanship_dum)) %>% # independent = 3
  select(-Q5.2, -Q6.2, -Q7.2) %>% 
  mutate(Q9.2 = ifelse(Q9.2 == 1, 1, 0),
         Q9.3 = ifelse(Q9.3 == 3, 1, 0),
         Q9.4 = ifelse(Q9.4 == 2, 1, 0),
         Q9.5 = ifelse(Q9.5 == 2, 1, 0),
         Q9.6 = ifelse(Q9.6 == 4, 1, 0)) %>% 
  mutate(knowledge = Q9.2 + Q9.3 + Q9.4 + Q9.5 + Q9.6) %>% 
  mutate(knowledge_dum = ifelse(knowledge < 3, 1, 2)) %>%  # 1 = low, 2 = high
  mutate(education_dum = ifelse(education < 5, 1, 2)) %>%  # 1 = low, 2 = high
  mutate(interest_dum = ifelse(interest < 3, 1, 2)) %>% # 1 = low, 2 = high
  mutate(age_dum = ifelse(age < 7, 1, 2)) %>% # 1 = young, 2 = old
  mutate(trump_dum = ifelse(trump < 3, 1, 2)) %>% # 1 = pro, 2 = anti
  select(-Q9.2, -Q9.3, -Q9.4, -Q9.5, -Q9.6) %>% 
  select(-contains("Q3.")) %>% 
  select(-X)

save.image("temp/data_for_summary_stats_donors.RData")

# Attributes and Levels ---------------------------------------------------

clean_conjoint_data <- function(alphabet){
  
  code <- paste0(alphabet, ".")
  
  conjoint_wide <- survey %>%
    select("id" = V1, contains(code))
  
  temp1 <- conjoint_wide %>%
    select(id, contains(code)) %>% 
    gather("code", "name", 2:ncol(.)) %>% 
    filter(str_detect(code, paste0(alphabet, ".\\d+.\\d+$"))) %>% 
    separate(code, into = c("x", "task", "attribute"), sep = "\\.") %>%
    select(-x) %>%
    rename(attribute_name = name)
  
  temp2 <- conjoint_wide %>%
    select(id, contains(code)) %>% 
    gather("code", "name", 2:ncol(.)) %>% 
    filter(str_detect(code, paste0(alphabet, ".\\d+.\\d+.\\d+"))) %>%
    separate(code, into = c("x", "task", "profile", "attribute"), sep = "\\.") %>%
    select(-x) %>%
    rename(level_name = name)
  
  attribute_levels <- left_join(temp1, temp2) %>%
    select(-attribute) %>% 
    mutate_at(c("task", "profile"), .funs = as.numeric) %>% 
    rename(attribute = attribute_name,
           level = level_name) %>% 
    spread(attribute, level, fill = NA, convert = TRUE)
  
}

conjoint_wide_F <- survey %>% 
  select("id" = V1, contains("F."))

conjoint_wide_K <- survey %>% 
  select("id" = V1, contains("K."))

attribute_levels <- clean_conjoint_data("F") %>% 
  full_join(clean_conjoint_data("K"))

remove(conjoint_wide_F, conjoint_wide_K)

# Add demographics --------------------------------------------------------

attribute_levels <- attribute_levels %>% 
  mutate(Name = str_to_lower(Name) %>% str_trim())

demographics <- read_xlsx("data/candidate_names.xlsx") %>% 
  mutate(Name = str_to_lower(Name) %>% str_trim()) %>% 
  mutate(Gender = if_else(str_detect(Group, "M"), "Male", "Female"),
         Race = "White",
         Race = if_else(str_detect(Group, "B"), "Black", Race),
         Race = if_else(str_detect(Group, "L"), "Hispanic", Race))

attribute_levels <- attribute_levels %>% 
  left_join(demographics, by = "Name") %>% 
  select(id, task, profile, Partisanship, Gender, Race, everything(), -Group, -Name) %>% 
  mutate(id = as.character(id))

# Outcomes ----------------------------------------------------------------

outcomes <- paste0("Q3.", seq(from = 4, to = 31, by = 3)) 

responses <- survey %>%
  select("id" = V1, outcomes) %>% 
  gather("task", "value", 2:ncol(.)) %>% 
  mutate(task = str_replace_all(task, "Q3.", "") %>% 
           as.numeric() %>% 
           as.factor() %>% 
           as.numeric() 
  ) 

# Merge and wrangle data ---------------------------------------------------

df_donors <- attribute_levels %>% 
  left_join(responses) %>% 
  left_join(respondents) %>%
  
  # Recode outcome variables
  mutate(selected = ifelse(profile == value, 1, 0)) %>% 
  select(-value) %>%
  
  # Select and sort variables
  select(id, start, end, #duration,
         task, profile, selected, 
         everything()) %>% 
  
  # Rename attributes and levels
  rename(`Voting` = `Platform: Elections`,
         `Compromise` = `Platform: Compromise`,
         `Courts` = `Platform: Courts`,
         `Investigations` = `Platform: Investigations`,
         `Discrimination` = `Platform: Discrimination`,
         `Taxes` = `Platform: Limited government`) %>%
  mutate(`Voting` = 
           gsub("Supports new legislation to require voters to show state-issued ID at the polls.",
                "Supports voter ID laws",
                `Voting`)) %>% 
  mutate(`Voting` = 
           gsub("Opposes new legislation to require voters to show state-issued ID at the polls.",
                "Opposes voter ID laws",
                `Voting`)) %>% 
  mutate(`Compromise` = 
           gsub("Promises to stand up to the other party.",
                "Stand up to other party",
                `Compromise`)) %>% 
  mutate(`Compromise` = 
           gsub("Promises to work for compromise across party lines.",
                "Supports compromise",
                `Compromise`)) %>% 
  mutate(`Courts` = 
           gsub("Said elected officials must obey the courts even when they think that the decisions are wrong.",
                "Obey courts",
                `Courts`)) %>% 
  mutate(`Courts` = 
           gsub("Said elected officials should not be bound by court decisions they regard as politicized.",
                "Disregard politicized decisions",
                `Courts`)) %>% 
  mutate(`Investigations` = 
           gsub("Said elected officials should supervise law enforcement investigations of politicians and their associates.",
                "Partisan involvement",
                `Investigations`)) %>% 
  mutate(`Investigations` = 
           gsub("Said law enforcement investigations of politicians and their associates should be free of partisan influence.",
                "Independent",
                `Investigations`)) %>% 
  mutate(`Discrimination` = 
           gsub("Believes discrimination against racial minorities is less of a problem now than in the past.",
                "Not a big problem",
                `Discrimination`)) %>% 
  mutate(`Discrimination` = 
           gsub("Believes the government should do more to prevent discrimination against racial minorities.",
                "Prevent discrimination",
                `Discrimination`)) %>% 
  mutate(`Taxes` = 
           gsub("Wants to lower taxes on everyone, including the wealthy.",
                "Less progressive",
                `Taxes`)) %>% 
  mutate(`Taxes` = 
           gsub("Wants to raise taxes on the wealthy.",
                "More progressive",
                `Taxes`)) %>% 
  
  # Change attributes to factors
  mutate_at(.vars = c("Partisanship", 
                      "Gender",
                      "Race",
                      "Compromise", 
                      "Courts",
                      "Discrimination",
                      "Voting",
                      "Investigations",
                      "Taxes"), funs(as.factor(.)))

# Save data.frame  --------------------------------------------------------

# Save data after dropping missing outcome variables

df_missing_donors <- df_donors %>%
  filter(is.na(selected)) 

df_cleaned_donors <- df_donors %>%
  filter(!is.na(selected))

# Remove unncessary objects

rm(list= ls()[!(ls() %in% c("df_cleaned_donors"))])

save.image("temp/data_for_analysis_donors.RData")
